<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/information/address' }">
        <i class="el-icon-caret-right"></i> 收货地址</el-breadcrumb-item>
      <el-breadcrumb-item>添加</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="收货人" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="省份" prop="addressId">
        <el-select v-model="ruleForm.addressId"  placeholder="请选择">
          <el-option
              v-for="item in provincesOptions"
              :key="item.pid"
              :label="item.provincial"
              :value="item.pid">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="detailed">
        <el-input v-model="ruleForm.detailed"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      provincesOptions: [],
      ruleForm: {
        name: '',
        addressId:'',
        detailed:'',
        phone:''
      },
      rules: {
        name: [
          {required: true, message: '请输入收货人', trigger: 'blur'},
        ],
        addressId: [
          {required: true, message: '请选择省份', trigger: 'blur'},
        ],
        detailed: [
          {required: true, message: '请输入详细地址', trigger: 'blur'},
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    loadProvincesOptionsList(){
      let url = 'http://localhost:8080/provincial/list';
      this.axios.get(url).then((response)=>{
        this.provincesOptions=response.data.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/address/addNew';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {'arrayFormat': 'repeat'});
          console.log('formData = ' + formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：');
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadProvincesOptionsList();
  }
}
</script>

<style scoped>

</style>